﻿using BootstrapBlazor.Components;
using PuppeteerSharp;
using SubStationManagement.Server.Models;

namespace SubStationManagement.Server.Services
{
    internal class LookupService(IServiceProvider provider,
    TargetService _TargetService,
    MeasureModuleService _MeasureModuleService,
    DIDOModuleService _DIDOModuleService,
    RecordingModuleService _RecordingModuleService) : LookupServiceBase
    {
        private IServiceProvider Provider { get; } = provider;

        private TargetService targetService = _TargetService;
        private MeasureModuleService measureModuleService = _MeasureModuleService;
        private DIDOModuleService dIDOModuleService = _DIDOModuleService;
        private RecordingModuleService recordingModuleService = _RecordingModuleService;

        public override IEnumerable<SelectedItem>? GetItemsByKey(string? key, object? data)
        {
            IEnumerable<SelectedItem>? items = null;
            if (key == "Target.Name")
            {
                List<SelectedItem> itemList = new List<SelectedItem>();
                List<Models.Target> targets = targetService.GetAll();

                foreach (var target in targets)
                {
                    itemList.Add(new() { Value = target.Id.ToString(), Text = target.Name });
                }
                items = itemList.AsEnumerable();
            }

            if (key == "MeasureModule.Name")
            {
                List<SelectedItem> itemList = new List<SelectedItem>
            {
                new() { Value = "0", Text = "不选择" }
            };
                List<MeasureModule> modules = measureModuleService.GetAll();

                foreach (var module in modules)
                {
                    itemList.Add(new() { Value = module.Id.ToString(), Text = module.SerialNumber });
                }
                items = itemList.AsEnumerable();
            }

            if (key == "DIDOModule.Name")
            {
                List<SelectedItem> itemList = new List<SelectedItem>
            {
                new() { Value = "0", Text = "不选择" }
            };
                List<DIDOModule> modules = dIDOModuleService.GetAll();

                foreach (var module in modules)
                {
                    itemList.Add(new() { Value = module.Id.ToString(), Text = module.SerialNumber });
                }
                items = itemList.AsEnumerable();
            }

            if (key == "RecordingModule.Name")
            {
                List<SelectedItem> itemList = new List<SelectedItem>
            {
                new() { Value = "0", Text = "不选择" }
            };
                List<RecordingModule> modules = recordingModuleService.GetAll();

                foreach (var module in modules)
                {
                    itemList.Add(new() { Value = module.Id.ToString(), Text = module.SerialNumber });
                }
                items = itemList.AsEnumerable();
            }
            return items;
        }
    }
}
